<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-03-29
 * Time: 11:34
 */


include_once(APP_ROOT.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'function'.DIRECTORY_SEPARATOR.'dir.func.php');
/**
 * 对用户的密码进行加密
 * @param $password
 * @param $encrypt //传入加密串，在修改密码时做认证
 * @return array/password
 */
function password($password, $encrypt='') {
    $pwd = array();
    $pwd['encrypt'] =  $encrypt ? $encrypt : create_randomstr();
    $pwd['password'] = md5(md5(trim($password)).$pwd['encrypt']);
    return $encrypt ? $pwd['password'] : $pwd;
}

/**
 * 生成随机字符串
 * @param string $lenth 长度
 * @return string 字符串
 */
function create_randomstr($lenth = 6) {
    return random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
}

function LANG($language = 'no language', $pars = array(), $module = ''){

    static $LANG = array();

    static $LANG_MODULES = array();
    //现阶段使用默认语种
    static $lang = 'zh-cn';
    //引入lang文件
    require_once APP_BASE.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.'admin.lang.php';
    require_once APP_BASE.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.'system.lang.php';
    require_once APP_BASE.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.'system_menu.lang.php';
    if(file_exists(APP_PATH .DIRECTORY_SEPARATOR.MODULE_NAME.'/install/languages/'.$lang.'/'.MODULE_NAME.'.lang.php'))
        require_once APP_PATH .DIRECTORY_SEPARATOR.MODULE_NAME.'/install/languages/'.$lang.'/'.MODULE_NAME.'.lang.php';
    return $LANG[$language];
}

/**
 * @param $input
 * @return string
 * 输入过滤
 */
function filterInputArray($ia){
    foreach($ia as $k => $v){
        $ia[$k] = filterInput($v);
    }
    return $ia;
}

function filterInput($input){
    if(IS_GET){
        $input = I('get.');
    }else if(IS_POST){
        $input = I('post.');
    }
    foreach($input as $k => $v){
        if($v == '')
            $input[$k] = null;
    }
    return $input;
}

//整数过滤
function filterIntenger($input){
    $input = filterInput($input);
    $input = filterInput($input);
    if(!isset($input)){
        throw new Exception('id未设定');
    }else if(!is_numeric($input)){
        throw new Exception('id格式错误');
    }
    return $input;
}

function getNowTime(){
    return date('Y-m-d H:i:s');
}

function getNowDate_zhcn(){
    return date('Y年m月d日');
}

function getArrayString($array){
    $string = '';
    foreach($array as $k => $v){
        $string .= $k.'='.$v.'&&&&&&&&&&&&&&&';
    }
    return $string;
}

/**
 * @param $array
 * @return mixed
 * 这个方法用作使用table分页时，搜索参数的绑定
 */
function get_page_query_map($array){
    if(!isset($array)||!$array){
        $array = array();
    }
    //默认采用like
    //保留分页key为‘p’
    unset($array['page']);
    unset($array['rows']);
    $map = array();
    foreach($array as $k => $v){
        $map[$k] = array('like', "%$v%");
    }
    return $map;
}

function get_date_grid($model_name){
    $model = D($model_name);
    $map = array_merge(get_page_query_map(I('get.')), get_page_query_map(I('post.')));
    $page = max(I('get.page'), 1);
//    $rows_num = I('get.rows')?I('get.rows'):20;
    $rows_num = get_kv_val('admin_page_list_num')?get_kv_val('admin_page_list_num'):I('get.rows');
    $obj = $model->get_list_use_page($map, $page, $rows_num);
    return array('total'=>$obj['total'], 'rows'=>$obj['list'], 'map'=> $obj['map'], 'footer'=>$obj['footer']);
}

function filter_query_map($map = array()){
    $temp = array();
    unset($map['sort']);
    unset($map['order']);
    foreach($map as $k => $v){
        if($v[1]!='%%'&&$v[1]!=''){
            $temp[$k] = $v;
        }
    }
    return $temp;
}

function filter_query_map_get_order_str($map = array(), $order){
    $temp = array();
    $order_str = '';
    if(isset($map['sort'])){
        $order_str = str_replace('%', '', $map['sort'][1]).' ';
        if(isset($map['order'])){
            $order_str .= str_replace('%', '', $map['order'][1]);
        }else{
            $order_str .= ' desc';
        }
    }else{
        $order_str = $order;
    }
    unset($map['sort']);
    unset($map['order']);
    foreach($map as $k => $v){
        if($v[1]!='%%'&&$v[1]!=''){
            $temp[$k] = $v;
        }
    }
    return array('map'=>$temp, 'order_str'=>$order_str);
}

function set_module_session($module, $key, $value){
    $_SESSION[get_kv_val('session_pix').$module][$key] = $value;
}

function get_module_session($module, $key){
    $value = $_SESSION[get_kv_val('session_pix').$module][$key];
    return $value?$value:false;
}

function destory_module_session($module){
    $_SESSION[get_kv_val('session_pix').$module] = null;
    unset($_SESSION[get_kv_val('session_pix').$module]);
}

function get_kv_val($key){
    static $model;
    if(!$model||!isset($model))
        $model = D('KeyToValue');
    if(get_cache($key))
        $one = get_cache($key);
    else
        $one = $model->get_value($key);
    return $one?$one:false;
}

function set_cache($key, $valve, $time){
    S($key,$valve,$time);
}

function get_cache($key){
    return S($key)?S($key):false;
}

function destroy_cache($key){
    S($key,null);
}

function filter_path($path){
    return $path?$path:'/Public/img/no_pic.jpg';
}

/**
 * @param $module
 * @param $action
 *
 */
function extend_tag($module, $controller, $action, $parm = array()){
    $module = A($module.'/'.$controller);
    if(empty($parm))
        return $module->$action();
    else
        return $module->$action($parm);
}

function sub_num_text($str, $len){
    if($len < strlen($str))
        $str = substr($str,0,$len).'...';
    return $str;
}

/**
 * @param $filename
 * @return string
 * 文件名称中文字符转化
 */
function file_zh_cn_icvon($filename){
    //windows环境下，文件名最好是有GBK转化为utf-8
    if(IS_WIN){
        return iconv('UTF-8', 'GBK', $filename);
    }
    return $filename;
}
